class Utils {

    /**
     * 获取随机数
     * @param min 最小值
     * @param max 最大值
     * @returns 返回随机数
     */
    random = (min: number, max: number) => {

        return Math.floor(Math.random() * (max - min)) + min;

    }
    /**
     * 获取一个Empty的guid（uuid）
     * @returns 00000000-0000-0000-0000-000000000000
     */
    GuidEmpty = () => {

        return "00000000-0000-0000-0000-000000000000";
    }
    /**
     * 获取一个guid
     * @returns 3975624C-148F-4838-88C9-12AF85D2E05E
     */
    GenNonDuplicateID = () => {


        return (this.S4() + this.S4() + "-" + this.S4() + "-" + this.S4() + "-" + this.S4() + "-" + this.S4() + this.S4() + this.S4());

    }
    /**
     * 内部方法
     * @returns 随机字母或数字
     */
    S4 = () => {
        return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
    }
    /**
     * 获取url中的参数getRequest().id
     * @returns any
     */
    getRequest = ():any => {
        let url = window.location.search; //获取url中"?"符后的字串
        let theRequest = new Object();
        if (url.indexOf("?") != -1) {
            let str = url.substr(1);
            let strs = str.split("&");
            for (let i = 0; i < strs.length; i++) {
                //就是这句的问题
                theRequest[strs[i].split("=")[0]] = decodeURI(strs[i].split("=")[1]);
            }
        }
        return theRequest;
    }
    /**
 * 获取数据差异
 * 
 * @param arr1 数组
 * @param arr2 数组
 */
    diff = (arr1: any[], arr2: any[]) :any=> {
        arr1 = Array.from(new Set(arr1));
        arr2 = Array.from(new Set(arr2));
        var mergeArr = arr1.concat(arr2);
        return mergeArr.filter((x) => !(arr1.includes(x) && arr2.includes(x)));
    }

    /**
 * 获取当前节点
 *  
 * @param list 集合
 * @param id 节点编号
 */
    getNode = (list: any[], id: string):any => {
        for (let i in list) {
            let item = list[i];
            if (item.id === id) {
                return item;
            } else {
                if (item.children) {
                    let value = this.getNode(item.children, id);
                    if (value) {
                        return value;
                    }
                }
            }
        }
    }

    /**
     * 获取所有父节点 ( 包含当前节点 )
     * 
     * @param list 集合
     * @param id 节点编号
     */
    getParents = (list: any[], id: string) :any=> {
        for (let i in list) {
            if (list[i].id === id) {
                return [list[i]]
            }
            if (list[i].children) {
                let node = this.getParents(list[i].children, id)
                if (node !== undefined) {
                    return node.concat(list[i])
                }
            }
        }
    }

    treeToList = (arr: any[]):any => {
        let res: any[] = []
        let fn = (source: any[]) => {
            source.forEach(el => {
                res.push(el)
                el.children && el.children.length > 0 ? fn(el.children) : "";
            });
        };
        fn(arr);
        return res;
    }

    listToTree = (arr: any[]):any => {
        arr.forEach(e => {
            arr.forEach(y => {
                if (y.parentId == e.id) {
                    if (!e.children) {
                        e.children = []
                    }
                    e.children.push(y)
                }
            })
        })
        arr = arr.filter(ele => ele.parentId === null)
        return arr
    }

    editmenu = (arr: any[]):any => {
        var obj = [] as any;
        arr.forEach(e => {
            if (e.tag != "") {
                e.id = e.tag;

            }
            obj.push(e);
            if (e.children.length > 0) {
                var a = [] as any;
                e.children.forEach((element: any) => {

                    if (element.tag != "") {

                        if (element.tag.indexOf("?") > -1) {
                            element.id = element.tag + "&&appid=" + element.id;
                        } else {
                            element.id = element.tag + "?appid=" + element.id;
                        }

                    }
                    a.push(element)
                });
                //obj.children=a;
            }
        })
    }

}
export default new Utils();